package cn.kgc.crowdfundingadminspringboot3.controller;

import cn.kgc.crowdfundingadminspringboot3.common.ResultEntity;
import cn.kgc.crowdfundingadminspringboot3.pojo.Advertisement;
import cn.kgc.crowdfundingadminspringboot3.service.AdvertisementService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/advertisement")
public class AdvertisementController {

    @Resource
    AdvertisementService advertisementService;

    @RequestMapping("/to/page.html")
    public String toPage(){
        return "advertisement-page";
    }

    @RequestMapping("/search/by/keyword")
    @ResponseBody
    public ResultEntity<PageInfo<Advertisement>> search(@RequestParam(value="pageNum", defaultValue="1",required = false) Integer pageNum,
                                                        @RequestParam(value="pageSize", defaultValue="5",required = false) Integer pageSize,
                                                        @RequestParam(value="keyword", defaultValue="",required = false) String keyword){
        // 1.查询得到PageInfo对象
        PageInfo<Advertisement> pageInfo = advertisementService.queryForKeywordWithPage(pageNum, pageSize, keyword);

        // 2.封装结果对象返回
        return ResultEntity.successWithoutData(pageInfo);
    }
    //获取批量删除的id
    @ResponseBody
    @RequestMapping("/get/list/by/id/list")
    public ResultEntity<List<Advertisement>> getRoleListByIdList(@RequestBody List<Integer> roleIdList) {

        List<Advertisement> roleList = advertisementService.getadvertisementListByIdList(roleIdList);

        return ResultEntity.successWithoutData(roleList);
    }

    //确定批量删除
    @ResponseBody
    @RequestMapping("/batch/remove")
    public ResultEntity<String> batchRemove(@RequestBody List<Integer> roleIdList) {

        advertisementService.batchRemove(roleIdList);

        return ResultEntity.successWithoutData();
    }

    //添加
    @ResponseBody
    @RequestMapping("/save/role")
    public ResultEntity<String> saveRole(@RequestParam("advertisementName") String advertisementName) {

        advertisementService.saveRole(advertisementName);

        return ResultEntity.successWithoutData();
    }

    //获取修改数据
    @ResponseBody
    @RequestMapping("/update/role")
    public ResultEntity<String> updateRole(Advertisement advertisement) {

        advertisementService.updateRole(advertisement);

        return ResultEntity.successWithoutData();
    }
}
